var monitorTabs = [];
var counter = 1;
var mainModel = Backbone.Model.extend({
    defaults: {
        isStarted: false
    },
    initialize: function () {
        monitoretTabs = new tabsData;
    },
    connectToApp:function() {
        jQuery.support.cors = true;
        $.connection.hub.url = 'http://localhost:8082/signalr';
        var myHub = $.connection.myHub;
        $.connection.hub.error(function (error) {
            alert("An error occured " + error);
            console.log(error);
        });
        $.connection.hub.start({ jsonp: true }, function () { }).done(function () { extensionModel.set({ isStarted: true }); }).fail(function () {
            alert("Could not Connect!");
            console.log("Could not Connect");
        });
        myHub.ReloadTab = function(data) {
            console.log("Reload tab started " + data);
            monitoretTabs.refreshTabs();
        };
    }
});
var tabData = Backbone.Model.extend({});
var tabsData = Backbone.Collection.extend({
    model: tabData,
    refreshTabs:function() {
        this.each(function(tabdata) {
            chrome.tabs.executeScript(tabdata.get('tabID'), { code: 'location.reload(true);' }, function () { });
        });
    },
});

function onInitialize() {
    extensionModel = new mainModel;
    chrome.tabs.getAllInWindow(null, function (tabs) { onInitializeIcon(tabs); });
    chrome.tabs.onCreated.addListener(function (tab) { onCreateTabUpdated(tab); });
    chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { onTabUpdated(tabId, changeInfo, tab); });
    //chrome.extension.onMessage.addListener(function (request, sender, sendResponse) { onRequest(request, sender, sendResponse); });
    chrome.pageAction.onClicked.addListener(function (tab) {onClick(tab);});
}

function onClick(tab) {
    var clicketTab = new tabData({ tabID: tab.id });
    monitoretTabs.add(clicketTab);
    if (extensionModel.get('isStarted') == false) {
        extensionModel.connectToApp();
    }
}
tabsData.prototype.add = function (tab) {
    var isDupe = this.any(function (_tab) {
        return _tab.get('tabID') === tab.get('tabID');
    });
    if (isDupe) {
        chrome.pageAction.setIcon({ tabId: tab.get('tabID'), path: 'images/refresh-off.png' });
        return false;
    }
    chrome.pageAction.setIcon({ tabId: tab.get('tabID'), path: 'images/refresh-on.png' });
    Backbone.Collection.prototype.add.call(this, tab);
}
function onCreateTabUpdated(tab) {
    showIcon(tab);
}
function onTabUpdated(tabId, changeInfo, tab) {
    showIcon(tab);
}
function onInitializeIcon(tabs) {
    for (var t = 0; t < tabs.length; t++) {
        showIcon(tabs[t]);
    }
    
}
function showIcon(tab) {
    if (!isChromeUrl(tab.url)) {
        chrome.pageAction.show(tab.id);
    }
}

function isChromeUrl(url) {
    if (url.indexOf("chrome:") > -1) {
		return true;
	}
	else {
		return false;
	}
}
function isLocalhostUrl(url) {
    if (url.indexOf("localhost:") > -1) {
        return true;
    }
    else {
        return false;
    }
}